using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using Common;
using Common.Command;
using Common.Enum;
using Controls;
using Report;
using SExperiment;
using SSettings;

namespace MainView.MainControls
{
	public partial class UCPatientInfo : UserControl, IComponentConnector
	{
		public static readonly DependencyProperty IsDecryptProperty = DependencyProperty.Register("IsDecrypt", typeof(bool), typeof(UCPatientInfo), new UIPropertyMetadata(true));

		public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IList<Sample>), typeof(UCPatientInfo), new UIPropertyMetadata(null));

		public static readonly DependencyProperty GridColumnsProperty = DependencyProperty.Register("GridColumns", typeof(GridColumns), typeof(UCPatientInfo), new PropertyMetadata(null, OnGridColumnsChanged));

		public static readonly DependencyProperty ComboBoxItemsSourceProperty = DependencyProperty.Register("ComboBoxItemsSource", typeof(Hashtable), typeof(UCPatientInfo), new UIPropertyMetadata(new Hashtable()));

		public bool IsDecrypt
		{
			get
			{
				return (bool)GetValue(IsDecryptProperty);
			}
			set
			{
				SetValue(IsDecryptProperty, value);
			}
		}

		public IList<Sample> ItemsSource
		{
			get
			{
				return (IList<Sample>)GetValue(ItemsSourceProperty);
			}
			set
			{
				SetValue(ItemsSourceProperty, value);
			}
		}

		public GridColumns GridColumns
		{
			get
			{
				return (GridColumns)GetValue(GridColumnsProperty);
			}
			set
			{
				SetValue(GridColumnsProperty, value);
			}
		}

		public Hashtable ComboBoxItemsSource
		{
			get
			{
				return (Hashtable)GetValue(ComboBoxItemsSourceProperty);
			}
			set
			{
				SetValue(ComboBoxItemsSourceProperty, value);
			}
		}

		private static void OnGridColumnsChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCPatientInfo uCPatientInfo = sender as UCPatientInfo;
			if (uCPatientInfo != null)
			{
				uCPatientInfo.ResetColumns();
			}
		}

		public void CommitEdit()
		{
			dataGrid.CommitEdit();
		}

		public List<Sample> GetSelectSamples()
		{
			return dataGrid.SelectedCells.Select((DataGridCellInfo s) => s.Item).Distinct().Cast<Sample>()
				.ToList();
		}

		public UCPatientInfo()
		{
			InitializeComponent();
			dataGrid.SystemClipboard = true;
			MenuItem temp = new MenuItem
			{
				Header = Utility.GetMessage("Delete"),
				Command = new SimpleDelegateCommand(CommandBindingDelete_Executed, CommandBindingDelete_CanExecute)
			};
			dataGrid.addMenu(temp);
			dataGrid.IsLineCopy_Paste_Visable = true;
		}

		public bool CommandBindingDelete_CanExecute()
		{
			if (dataGrid.SelectedCells.Where((DataGridCellInfo s) => !s.Column.IsReadOnly).Count() > 0)
			{
				return IsDecrypt;
			}
			return false;
		}

		public void CommandBindingDelete_Executed()
		{
			if (MessageBox.Show(Utility.GetMessage("msg_delete_question"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No) != MessageBoxResult.Yes)
			{
				return;
			}
			foreach (DataGridCellInfo selectedCell in dataGrid.SelectedCells)
			{
				if (selectedCell.Column.IsReadOnly)
				{
					continue;
				}
				Sample sample = selectedCell.Item as Sample;
				if (sample == null)
				{
					continue;
				}
				string text = "WellCell.Well.Sample." + selectedCell.Column.SortMemberPath;
				if (text.IndexOf("WellCell.Well.Sample.PatientInfo.ID") != -1)
				{
					sample.PatientInfo.ID = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.Name") != -1)
				{
					sample.PatientInfo.Name = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.Sex") != -1)
				{
					sample.PatientInfo.Sex = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.Age") != -1)
				{
					sample.PatientInfo.Age = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.SpecimenDate") != -1)
				{
					sample.PatientInfo.SpecimenDate = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.Specimen") != -1)
				{
					sample.PatientInfo.Specimen = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.Doctor") != -1)
				{
					sample.PatientInfo.Doctor = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.Office") != -1)
				{
					sample.PatientInfo.Office = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.Diagnosis") != -1)
				{
					sample.PatientInfo.Diagnosis = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.CaseID") != -1)
				{
					sample.PatientInfo.CaseID = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.BedID") != -1)
				{
					sample.PatientInfo.BedID = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.InPatientID") != -1)
				{
					sample.PatientInfo.InPatientID = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.OutPatientID") != -1)
				{
					sample.PatientInfo.OutPatientID = null;
				}
				else if (text.IndexOf("WellCell.Well.Sample.PatientInfo.Remark") != -1)
				{
					sample.PatientInfo.Remark = null;
				}
				else if (selectedCell.Column.SortMemberPath.IndexOf("PatientInfo.CustomData") != -1)
				{
					int num = selectedCell.Column.SortMemberPath.IndexOf("PatientInfo.CustomData[") + "PatientInfo.CustomData[".Length;
					int num2 = selectedCell.Column.SortMemberPath.IndexOf("]", num);
					int num3 = Convert.ToInt32(selectedCell.Column.SortMemberPath.Substring(num, num2 - num));
					if (num3 < sample.PatientInfo.CustomData.Count)
					{
						sample.PatientInfo.CustomData[num3] = null;
					}
				}
			}
			dataGrid.RaiseItemUpdateEvent();
		}

		private string GetCustomFieldName(ColumnInfo info)
		{
			if (info.IsCustomColumn())
			{
				return "PatientInfo.CustomData[" + info.FieldIndex + "]";
			}
			if (string.IsNullOrEmpty(info.FieldName))
			{
				return null;
			}
			if (info.FieldName == "WellCell.Well.Project.BasicInfo.Name")
			{
				return info.FieldName.Replace("WellCell.Well.", "");
			}
			return info.FieldName.Replace("WellCell.Well.Sample.", "");
		}

		private void ResetColumns()
		{
			for (int num = dataGrid.Columns.Count - 1; num >= 1; num--)
			{
				DataGridCustomTemplateColumn column = dataGrid.Columns[num] as DataGridCustomTemplateColumn;
				if (column != null)
				{
					List<ColumnInfo> list = GridColumns.Columns.Where((ColumnInfo s) => s.Visibility && s.FieldName == column.Key).ToList();
					if (list.Count == 0)
					{
						dataGrid.Columns.RemoveAt(num);
					}
				}
			}
			if (GridColumns == null || GridColumns.Columns.Count == 0)
			{
				return;
			}
			int num2 = 1;
			foreach (ColumnInfo column2 in GridColumns.Columns)
			{
				if (column2.FieldName != "WellCell.Well.Project.BasicInfo.Name" && ((column2.Info & 4) != 4 || !column2.Visibility))
				{
					continue;
				}
				string bindingPath = GetCustomFieldName(column2);
				if (!string.IsNullOrEmpty(bindingPath))
				{
					ComboBoxItemsSource[bindingPath] = new List<string>(column2.DataSource);
					DataGridCustomTemplateColumn dataGridCustomTemplateColumn = null;
					List<DataGridCustomTemplateColumn> list2 = (from DataGridCustomTemplateColumn s in dataGrid.Columns.Where((DataGridColumn s) => s is DataGridCustomTemplateColumn)
						where s.Key == bindingPath
						select s).ToList();
					if (list2.Count == 1)
					{
						dataGridCustomTemplateColumn = list2[0];
					}
					else
					{
						dataGridCustomTemplateColumn = new DataGridCustomTemplateColumn();
						dataGrid.Columns.Add(dataGridCustomTemplateColumn);
					}
					dataGridCustomTemplateColumn.ClipboardContentBinding = new Binding(bindingPath)
					{
						UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
						Mode = BindingMode.TwoWay
					};
					dataGridCustomTemplateColumn.DisplayIndex = num2++;
					dataGridCustomTemplateColumn.Key = bindingPath;
					dataGridCustomTemplateColumn.SortMemberPath = bindingPath;
					dataGridCustomTemplateColumn.Header = column2.Header;
					dataGridCustomTemplateColumn.CellTemplate = DataTemplateCreator.CreateTextBlockDataTemplate(bindingPath);
					if (column2.FieldName == "WellCell.Well.Project.BasicInfo.Name")
					{
						dataGridCustomTemplateColumn.IsReadOnly = true;
					}
					switch (column2.Type)
					{
					case EColumnType.ComboBox:
						dataGridCustomTemplateColumn.CellEditingTemplate = DataTemplateCreator.CreateComboBoxDataTemplate(bindingPath, "ComboBoxItemsSource[" + bindingPath + "]", GetType().BaseType.Name, true);
						break;
					case EColumnType.DateTime:
						dataGridCustomTemplateColumn.CellEditingTemplate = DataTemplateCreator.CreateDatePickerDataTemplate(bindingPath);
						break;
					case EColumnType.TextBox:
						dataGridCustomTemplateColumn.CellEditingTemplate = DataTemplateCreator.CreateTextBoxDataTemplate(bindingPath);
						break;
					}
				}
			}
		}

		public bool CanCopy()
		{
			return dataGrid.CommandCopyCellCanExecute();
		}

		public void CopySelectedCellsToClipboard()
		{
			dataGrid.CommandCopyCellExecute();
		}

		public bool CanPaste()
		{
			return dataGrid.CommandPasteCellCanExecute();
		}

		public void PasteClipboardDataToCells()
		{
			dataGrid.CommandPasteCellExecute();
		}

		private List<List<string>> GetData(bool includeMergerCellInfo = false)
		{
			List<List<string>> list = new List<List<string>>();
			List<string> list2 = new List<string>();
			foreach (DataGridColumn column in dataGrid.Columns)
			{
				if (column.Visibility == Visibility.Visible)
				{
					list2.Add(column.Header.ToString());
				}
			}
			list.Add(list2);
			List<string> list3 = new List<string>();
			foreach (Sample item in (IEnumerable)dataGrid.Items)
			{
				list2 = new List<string>();
				foreach (DataGridColumn column2 in dataGrid.Columns)
				{
					if (column2.Visibility != 0)
					{
						continue;
					}
					if (column2.SortMemberPath.IndexOf("WellNames") != -1)
					{
						list2.Add(item.WellNames);
					}
					else if (column2.SortMemberPath.IndexOf("Project.BasicInfo.Name") != -1)
					{
						list2.Add(item.Project.BasicInfo.Name);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.ID") != -1)
					{
						list2.Add(item.PatientInfo.ID);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.Name") != -1)
					{
						list2.Add(item.PatientInfo.Name);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.Sex") != -1)
					{
						list2.Add(item.PatientInfo.Sex);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.Age") != -1)
					{
						list2.Add(item.PatientInfo.Age);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.SpecimenDate") != -1)
					{
						list2.Add(item.PatientInfo.SpecimenDate);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.Specimen") != -1)
					{
						list2.Add(item.PatientInfo.Specimen);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.Doctor") != -1)
					{
						list2.Add(item.PatientInfo.Doctor);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.Office") != -1)
					{
						list2.Add(item.PatientInfo.Office);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.Diagnosis") != -1)
					{
						list2.Add(item.PatientInfo.Diagnosis);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.CaseID") != -1)
					{
						list2.Add(item.PatientInfo.CaseID);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.BedID") != -1)
					{
						list2.Add(item.PatientInfo.BedID);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.InPatientID") != -1)
					{
						list2.Add(item.PatientInfo.InPatientID);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.OutPatientID") != -1)
					{
						list2.Add(item.PatientInfo.OutPatientID);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.Remark") != -1)
					{
						list2.Add(item.PatientInfo.Remark);
					}
					else if (column2.SortMemberPath.IndexOf("PatientInfo.CustomData") != -1)
					{
						int num = column2.SortMemberPath.IndexOf("PatientInfo.CustomData[") + "PatientInfo.CustomData[".Length;
						int num2 = column2.SortMemberPath.IndexOf("]", num);
						int num3 = Convert.ToInt32(column2.SortMemberPath.Substring(num, num2 - num));
						if (num3 < item.PatientInfo.CustomData.Count)
						{
							list2.Add(item.PatientInfo.CustomData[num3]);
						}
						else
						{
							list2.Add(string.Empty);
						}
					}
					if (list3.Count < list[0].Count)
					{
						list3.Add(string.Empty);
					}
				}
				list.Add(list2);
			}
			if (includeMergerCellInfo)
			{
				list.Insert(0, list3);
			}
			return list;
		}

		public void SaveToExcel(string file)
		{
			ExcelData excelData = new ExcelData();
			ExcelSheetData excelSheetData = new ExcelSheetData();
			excelSheetData.Name = Utility.GetMessage("sample_info");
			excelSheetData.Data = GetData(true);
			excelData.SheetDataList.Add(excelSheetData);
			ExcelOperator.SaveToExcel(file, excelData);
		}

		public void SaveToCsv(string file)
		{
			CsvTxtOperator.SaveToCsv(file, GetData());
		}

		private void dataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
		{
			if (!IsDecrypt)
			{
				e.Cancel = true;
			}
		}
	}
}
